﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CIS.Web.Common
{
	public static class StringExtension
	{
		public static string ToMD5(this string input)
		{
			using (MD5 md5Hash = MD5.Create())
			{
				// Convert the input string to a byte array and compute the hash.
				byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

				// Create a new Stringbuilder to collect the bytes
				// and create a string.
				StringBuilder sBuilder = new StringBuilder();

				// Loop through each byte of the hashed data 
				// and format each one as a hexadecimal string.
				for (int i = 0; i < data.Length; i++)
				{
					sBuilder.Append(data[i].ToString("x2"));
				}

				// Return the hexadecimal string.
				return sBuilder.ToString();
			}
		}

		public static string GetControllerName(this string controller)
		{
			return controller.Replace("Controller", string.Empty);
		}

		//默认密钥向量
		private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

		/// <summary>
		/// DES加密字符串
		/// </summary>
		/// <param name="encryptString">待加密的字符串</param>
		/// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
		public static string Encrypt(string encryptString)
		{
			try
			{
				string encryptKey = ConstValues.EncryptKey;

				byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
				byte[] rgbIV = Keys;
				byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
				DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
				MemoryStream mStream = new MemoryStream();
				CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
				cStream.Write(inputByteArray, 0, inputByteArray.Length);
				cStream.FlushFinalBlock();
				return Convert.ToBase64String(mStream.ToArray());
			}
			catch
			{
				return encryptString;
			}
		}

		/// <summary>
		/// DES解密字符串
		/// </summary>
		/// <param name="decryptString">待解密的字符串</param>
		/// <returns>解密成功返回解密后的字符串，失败返源串</returns>
		public static string Decrypt(string decryptString)
		{
			try
			{
				string decryptKey = ConstValues.EncryptKey;

				byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
				byte[] rgbIV = Keys;
				byte[] inputByteArray = Convert.FromBase64String(decryptString);
				DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
				MemoryStream mStream = new MemoryStream();
				CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
				cStream.Write(inputByteArray, 0, inputByteArray.Length);
				cStream.FlushFinalBlock();
				return Encoding.UTF8.GetString(mStream.ToArray());
			}
			catch
			{
				return decryptString;
			}
		}

		/// <summary>
		///  邮件地址加密
		/// </summary>
		/// <param name="encryptMail">待加密的邮件地址</param>
		/// <returns></returns>
		public static string EncryptMail(string encryptMail)
		{
			var mailArray = encryptMail.Split(ConstValues.chrAt);
			var encypt = string.Empty;
			for (var index = 0; index < mailArray[0].Length - 3; index++)
			{
				encypt += "*";
			}
			return mailArray[0].Substring(0, 3) + encypt + ConstValues.chrAt + mailArray[1];
		}
	}
}